import { AppConsts } from '@/models/AppConsts';
import storage from '@/utils/storage';
import { getAuthUrl } from '@/utils/utils';
import { createRouter, createWebHashHistory } from 'vue-router'

var routes = [
    { path: "/login", component: () => import('@/views/login/Login.vue'), meta: { anonymous: true, title: "绑定账号" } },
    { path: "/testlogin", component: () => import('@/views/login/TestLogin.vue'), meta: { anonymous: true, title: "模拟登录" } },
    { path: "/", component: () => import('@/views/index.vue'), meta: { title: "首页" } },
    { path: "/goods/list", component: () => import('@/views/goods/GoodsList.vue'), meta: { title: "商品列表" } },
    { path: "/xue/sync", component: () => import('@/views/xue/XueSync.vue'), meta: { title: "同步靴" } },
    { path: "/xue/list", component: () => import('@/views/xue/XueList.vue'), meta: { title: "靴列表" } }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});


router.beforeEach((to, _from, next) => {
    document.title = (to.meta.title as string);
    if (!to.meta.anonymous) {
        let token = storage.getStorage(AppConsts.USER_TKN);
        if (token) {
            next();
        } else {
            let url = getAuthUrl(to.path);
            console.log(url);
            //if (process.env.NODE_ENV === "development") {
            if (false) {
                router.replace("/userinfo/testlogin");

            } else {
                console.log(url);
                if (url) {
                    location.href = url;
                }
            }

        }
    } else {
        next();
    }
});

export default router;